<template>
  <div>
    <TopMenuBar></TopMenuBar>

    <div class="content">
      <img :src="'http://150.158.212.186:3001' + goods.src" alt="" class="goods-img">
      <div class="info">
        <p class="title">
          【{{goods.goods_category_name}}】{{goods.name}}-----{{goods.describe}}
        </p>
        <p class="address">发货地：{{goods.address}}</p>
        <p class="price">单价：￥{{goods.price}}</p>
        <p class="store">库存：{{goods.store}}</p>
        <p class="user-username">店家：{{goods.user_username}}</p>
      </div>
      <div class="option">
        <button class="sub" v-if="count > 0 && goods.store > 0" @click="() => {this.count--}">-</button>
        <input type="text" oninput="value=value.replace(/[^\d]/g,'')" v-model="count" :disabled="goods.store <= 0">
        <button class="add" v-if="count < goods.store" @click="() => {this.count++}">+</button>
        <button class="add-car" @click="mAddToCar">加入购物车</button>
      </div>
    </div>

  </div>
</template>

<script>
  import TopMenuBar from "components/content/topmenubar/TopMenuBar";

  import {verToken} from 'common/utils'
  import {mapGetters} from "vuex";

  export default {
    name: "GoodsDetail",
    components: {
        TopMenuBar,
    },
    data() {
      return {
        goods: {
          id: 0, // 商品id
          name: '', // 商品名
          describe: '', // 商品描述
          price: 0, // 商品单价
          store: 0, // 商品库存
          src: '', // 商品图片
          goods_category_id: 0, // 商品所属类目,通过该商品添加者的username得到
          goods_category_name: '', // 商品所属类目名
          address: '', // 发货地址
          index: 1, // 商品热度
          user_username: '', //该商品的商家用户名
        },
        count: 1,
        // count: this.goods.store > 0 ? 1 : 0,
      }
    },
    created() { // 组件创建之前
      this.goods = this.$route.params.goods;
      // console.log(this.goods);
    },
    computed: {
      ...mapGetters ({
        token: 'getToken'
      })
    },
    methods: {
      mAddToCar() {
        if (this.token) { // 如果token存在
          if (this.count >= 1 && this.count <= this.goods.store && this.goods.store >= 1) {
            let carObj = {
              id: this.goods.id,
              name: this.goods.name,
              describe: this.goods.describe,
              goods_price: this.goods.price,
              // goods_store: this.goods.store,
              src: this.goods.src,
              goods_category_id: this.goods.goods_category_id,
              index: this.goods.index,
              user_username: this.goods.user_username,

              goods_count: this.count,
            }
            console.log(carObj);
            this.$store.dispatch('aAddToCar', carObj)
              .then(res => {
                this.$mes_ty_toast.toShow(res, 2000)
              })
          } else { // 否则,即商品数量输入不合法
            this.$err_ty_toast.toShow('添加商品至购物车失败！！！商品数量输入非法或者库存不足', 2000)
          }
        } else { // 否则,即token不存在
          this.$router.push('/login')
          this.$mes_ty_toast.toShow('您还未登录,请先登录', 2000)
        }


      }
    }
  }
</script>

<style scoped>
  .content {
    width: 75%;
    /*height: 100vh;*/
    position: relative;
    margin: auto;

    background-color: white;
  }
  .content .goods-img {
    position: absolute;
    width: 30%;
    height: 280px;

    margin: 10px;

    border: 2px solid #cdcdcd;
  }
  .content .info {
    width: 65%;
    height: 300px;
    transform: translateX(50%);

    padding: 10px;
    /*background-color: red;*/
  }
  .content .info .title {

    font-size: 20px;
    font-weight: bold;
  }
  .content .info .address {
    margin: 20px;
    color: red;
  }
  .content .info .price {
    margin: 20px;
    color: red;
    font-weight: bold;
    font-size: 30px;
  }
  .content .info .store {
    color: #7b7b7b;
  }

  .content .option {
    position: relative;
    height: 200px;
    /*background-color: yellow;*/
  }
  .add-car {
    background-color: red;
    color: white;

    width: 300px;
    height: 50px;

    font-size: 20px;

    position: absolute;
    left: 50%;
    top: 50%;
    border-radius: 6px;
    border: none;
    outline: none;
  }
  button:hover {
    cursor: pointer;
    opacity: 0.75;
  }
  .option input {
    width: 30px;
    height: 50px;
    position: absolute;
    top: 50%;
    left: 45%;

    text-align: center;
  }
  .option .sub {
    height: 54px;
    width: 20px;
    position: absolute;
    top: 50%;
    left: 43.5%;
  }
  .option .add {
    height: 54px;
    width: 20px;
    position: absolute;
    top: 50%;
    left: 47.9%;
  }
</style>